//
//  TabBarView.m
//  SchoolTimetable
//
//  Created by 梦里都是梦 on 16/1/6.
//  Copyright (c) 2016年 Yuanlinqian. All rights reserved.
//

#import "TabBarView.h"


@interface TabBarView ()


@property (nonatomic, strong) UIButton *selecteBtn;
@end
@implementation TabBarView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addButton];
    }
    return self;
}



-(void)addButton
{
    NSArray *vcTitleArray = @[@"首页",@"课表",@"我"];
    NSArray *norImageArray = @[@"home_normal",
                               @"syllabus_normal",
                               @"person_normal"];
    //高亮状态图片
    NSArray *hlImageArray = @[@"home_light",
                              @"syllabus_light",
                              @"person_light"];
    
    //选中状态图片
    NSArray *selImageArray = @[@"home_selected",
                               @"syllabus_selected",
                               @"person_selected"];
    
    // 获取屏幕的尺寸
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    // 定制UITabBar
    self.backgroundColor = [UIColor colorWithRed:233/255.f green:215/255.f blue:186/255.f alpha:1];
    for (int index = 0; index <  vcTitleArray.count; index++) {
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(index * (screenSize.width/3), 0, screenSize.width/3, 60)];
        [self addSubview:button];
        
        [button setImage:[UIImage imageNamed:norImageArray[index]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:selImageArray[index]] forState:UIControlStateSelected];
        [button setImage:[UIImage imageNamed:hlImageArray[index]] forState:UIControlStateHighlighted];
        button.tag = index;
        
        // 添加按钮事件相应方法法
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
        if (!index) {
            button.selected = YES;
            _selecteBtn = button;
        }
    }
    // UILabel和UIImageView用户交互默认为NO
    self.userInteractionEnabled = YES;
}
#pragma mark - 按钮点击

- (void)buttonPressed:(UIButton *)btn
{

    _selecteBtn.selected = NO;
    btn.selected = YES;
    _selecteBtn = btn;
    
    NSInteger index = btn.tag;
    if (_passIndex) {
        _passIndex(index);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
